var importInfoInterval;
var isRunning = true;
//获取导入状态
function getImportInfo(){
	var url = $('importForm').action + '?doAction=getImportInfo&decorator=blank';
	$j.post(url,null,getImportInfoCallback,"json");
}
//获取导入状态 callback
function getImportInfoCallback(data){
	handleControll(data.status);
	$j('#completedPercent').css("width",data.completedPercent+"%");
	$j('#process').html(data.currentRow+"/"+data.importFileRowCount);
	$j('#importForm #status').val(data.status);
	$j('#importStatus').html(""+data.importStatus);
	$j('#successInsertCount').html(""+data.successInsertCount);
	$j('#failInsertCount').html(""+data.failInsertCount);
	$j('#successUpdateCount').html(""+data.successUpdateCount);
	$j('#failUpdateCount').html(""+data.failUpdateCount);
	$j('#ignoreCount').html(""+data.ignoreCount);
	$j('#continuouslyFailCount').html(""+data.continuouslyFailCount);
	$j('#beginRow').html(""+data.beginRow);
	$j('#endRow').html(""+data.endRow);
	$j('#failData').html(""+data.failData);
	$j('#endRow').html(""+data.endRow);
	$j('#startImportTime').html(data.startImportTime);
	$j('#endImportTime').html(data.endImportTime);
	
	if($j('#importFileUrl').html().indexOf('download2')==-1){
		$j('#importFileUrl').append('<span class="download2"></span>&nbsp;<a id="importFileUrlLink">'+__FMT.import_file_download+'</a>');
		var jLink =  $j('#importFileUrlLink');
		jLink.attr("href",__ctxPath+'/tool/import.html?doAction=downloadMedia&fileUrl='+data.importFileUrl);
		
	}
	if($j('#failFileUrl').html().indexOf('download2')==-1){
		if(data.failData.trim()!=""){
			$j('#failFileUrl').append('<span class="download2"></span>&nbsp;<a id="failFileUrlLink">'+__FMT.import_file_download+'</a>');
			var jLink =  $j('#failFileUrlLink');
			jLink.attr("href",__ctxPath+'/tool/import.html?doAction=downloadMedia&fileUrl='+data.failFileUrl);
		}	 
	}	
	if($j('#successFileUrl').length > 0 && $j('#successFileUrl').html().indexOf('download2')==-1){
		$j('#successFileUrl').append('<span class="download2"></span>&nbsp;<a id="successFileUrlLink">'+__FMT.import_file_download+'</a>');
		var jLink =  $j('#successFileUrlLink');
		jLink.attr("href",__ctxPath+'/tool/import.html?doAction=downloadMedia&fileUrl='+data.successFileUrl);
	}	
	if($j('#logFileUrl').html().indexOf('download2')==-1){
		$j('#logFileUrl').append('<span class="download2"></span>&nbsp;<a id="logFileUrlLink">'+__FMT.import_file_download+'</a>');
		var jLink =  $j('#logFileUrlLink');
		jLink.attr("href",__ctxPath+'/tool/import.html?doAction=downloadMedia&fileUrl='+data.logFileUrl);
	}	
	if($j('#failLogFileUrl').html().indexOf('download2')==-1){
		if(data.failData.trim()!=""){
			$j('#failLogFileUrl').append('<span class="download2"></span>&nbsp;<a id="failLogFileUrlLink">'+__FMT.import_file_download+'</a>');
			var jLink =  $j('#failLogFileUrlLink');
			jLink.attr("href",__ctxPath+'/tool/import.html?doAction=downloadMedia&fileUrl='+data.failLogFileUrl);
		}	
	}		
}
//处理按钮和逻辑
function handleControll(status){
	if(status == 1 || status ==-1 || status == 2){
		//导入完成,导入失败,用户主动停止
		disableIconBtn("stop");
		disableIconBtn("pause");
		disableIconBtn("resume");
		clearInterval(importInfoInterval);
	}else if(status == 0){
		//导入中
		enableIconBtn("stop");
		enableIconBtn("pause");
		disableIconBtn("resume");
	}else if(status == 3){
		//主动暂停
		enableIconBtn("stop");
		disableIconBtn("pause");
		enableIconBtn("resume");
	}
}

//停止导入
function stopImport(){
	if(confirm(__FMT.import_stop_confirm)){
		var url = $('importForm').action + '?doAction=stopImport&decorator=blank';
		$j.post(url,null,stopImportCallback);
		disableIconBtn('stop');
		disableIconBtn("pause");
		disableIconBtn("resume");
	}	
}
//停止导入后的callback函数
function stopImportCallback(){
	getImportInfo();
}

//暂停导入
function suspendImport(){
	var url = $('importForm').action + '?doAction=suspendImport&decorator=blank';
	$j.post(url,null,suspendImportCallback);
	enableIconBtn("stop");
	disableIconBtn("pause");
	enableIconBtn("resume");
}
//暂停导入后的callback函数
function suspendImportCallback(){
	getImportInfo();
}

//继续导入
function resumeImport(){
	var url = $('importForm').action + '?doAction=resumeImport&decorator=blank';
	$j.post(url,null,resumeImportCallback);
	enableIconBtn("stop");
	enableIconBtn("pause");
	disableIconBtn("resume");
}
//继续导入后的callback函数
function resumeImportCallback(){
	getImportInfo();
}
//重新开始任务
function restartImport(){
	if(confirm(__FMT.import_restart_confirm)){
		clearInterval(importInfoInterval);
		fnAddHiddenField("doAction","restartImport",$('importForm'));
		$('importForm').submit();
	}
}
//新任务
function newTask(){
	if(confirm(__FMT.import_newTask_confirm)){
		fnAddHiddenField("doAction","newTask",$('importForm'));
		$('importForm').submit();
	}
}

//下载文件
function downloadFile(fileUrl){
	fnAddHiddenField("doAction","downloadFile",$('importForm'));
	fnAddHiddenField("fileUrl",fileUrl,$('importForm'));
	fnAddHiddenField("isFrontMedia",1,$('importForm'));
	$('importForm').submit();
}